<!--
 * @Description: 
 * @Version: 
 * @Autor: ps
 * @Date: 2022-09-08 11:43:43
 * @LastEditors: ps
 * @LastEditTime: 2022-09-13 18:22:05
-->
<template>
  <div class="lineChart">
    <div id="budgetLine" :style="{ height: height + 'px', width: width }"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { LineChart } from "echarts/charts";
echarts.use([LineChart]);
export default {
  name: "lineChat",
  props: {
    // 折线图数据
    lineData: {
      type: Array,
      default: [],
    },
    xData: {
      type: Array,
      default: [],
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: Number,
      default: 200,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.lineCharts(this.lineData, this.xData);
  },
  beforeDestroy() {
    this.chartDes();
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    lineCharts(lineData, xData) {
      let nameArr = [];
      lineData.forEach((element) => {
        let obj = {
          name: element.name,
          icon: "roundRect",
          itemStyle: {
            opacity: 0.6,
            // color: element.lineStyle.color,
          },
        };
        nameArr.push(obj);
      });
      let option = {
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: xData,
          // axisLabel: {
          //   interval: '0',
          // },
        },
        grid: {
          left: "1%",
          right: "3%",
          bottom: "1%",
          top: "15%",
          containLabel: true,
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: nameArr,
        },
        series: lineData,
      };
      this.initChart("budgetLine", option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>
